package middlewareorid.ui;

import superwaba.ext.xplat.ui.MultiEdit;
import middlewareorid.engine.Config;
import middlewareorid.engine.HTMLConnect;
import waba.fx.Color;
import waba.fx.Font;
import waba.fx.Rect;
import waba.ui.Button;
import waba.ui.Container;
import waba.ui.ControlEvent;
import waba.ui.Edit;
import waba.ui.Event;
import waba.ui.Label;
import waba.ui.ListBox;
import waba.util.Vector;

public class ListBlogWindow extends Container {

	Button btDetail = new Button("  Detail  ");
	Button btDelete = new Button("  Delete  ");
	Button btBack = new Button("  Back  ");
	Font font = new Font("SW", Font.PLAIN, 10);
	ListBox lbBlogs = new ListBox();
	Label info = new Label("");
	Edit edTtl = new Edit();
	MultiEdit edDetail = new MultiEdit(10, 1);

	public ListBlogWindow(Rect rect) {
		setRect(rect);
		setBackColor(Color.WHITE);
		
		addListControl();
		list();
	}

	private void list() {
		/*
		 * HTML Class Test
		 */
		HTMLConnect con = new HTMLConnect();
		con.setURL("http://blogs.or.id/djokdja.php?djokdjaid=danarwidyantoro&djokdjaAction=queryblogs");
		Vector v = con.getListItem(con.getURLData());
		String datas[] = (String[]) v.toObjectArray();
		for (int i = 0; i < datas.length; i++) {
			lbBlogs.insert(datas[i], i);
		}				
	}

	public void onEvent(Event event) {
		switch (event.type) {
		case ControlEvent.PRESSED:
			if (event.target == btDetail) {
				addDetailControl();
			} else if (event.target == btBack) {
				addListControl();
			}
			break;
		}
	}
	
	public void setStyle(int style) {
		setBackColor(Config.background[style]);
	}
	
	private void addListControl() {
		remove(edTtl);
		remove(edDetail);
		remove(btBack);
		
		add(lbBlogs);
		lbBlogs.setRect(LEFT+5, TOP+5, getClientRect().width-10, getClientRect().height-30);
		add(btDetail, LEFT+5, AFTER+5);
		add(btDelete, AFTER+10, SAME);		
	}
	
	private void addDetailControl() {
		int start = 0;
		String sTemp = "";
		
		remove(lbBlogs);
		remove(btDetail);
		remove(btDelete);
		add(edTtl);
		if (lbBlogs.getSelectedIndex() < 0) lbBlogs.select(0);
		sTemp = lbBlogs.getSelectedItem().toString();
		start = sTemp.indexOf("++") + 2;
		sTemp = sTemp.substring(start, sTemp.length());
		edTtl.setText(sTemp);
		edTtl.setRect(LEFT+5, TOP+5, getClientRect().width-10, PREFERRED);
		edTtl.setEditable(false);
		add(edDetail);
		edDetail.setDrawDots(false);
		edDetail.setRect(LEFT+5, AFTER+5, getClientRect().width-10, getClientRect().height-60);
		add(btBack, LEFT+5, AFTER+5);
		
		start = lbBlogs.getSelectedItem().toString().indexOf("++");		
		edDetail.setText(queryDetail(lbBlogs.getSelectedItem().toString().substring(0, start)));
		edDetail.setEditable(false);
	}	

	private String queryDetail(String inumber) {		
		String result = "";
		HTMLConnect con = new HTMLConnect();
		con.setURL("http://blogs.or.id/djokdja.php?djokdjaid=danarwidyantoro&djokdjaAction=querydetails&inumber=" + inumber);
		result = con.getURLData();
		int start = result.indexOf("[LST]") + 5;
		int end = result.indexOf("[/LST]");
		result = result.substring(start, end);
		return result;
	}
}
